package com.hjq.mallmember.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther HJQ
 */
@Configuration
public class FeignInterceptor {
    @Bean
    public RequestInterceptor requestInterceptor(){
        RequestInterceptor interceptor = new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                //System.out.println("调用了这玩意");
                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
               if(attributes!=null){
                   HttpServletRequest oldRequest = attributes.getRequest();
                   //同步请求头数据中的Cookie
                   requestTemplate.header("Cookie",oldRequest.getHeader("Cookie"));
               }

            }
        };
        return  interceptor;
    }
}
